package com.andong.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.andong.domain.entity.Coach;
import com.andong.domain.entity.Student;
import com.andong.domain.pojo.Result;
import com.andong.domain.vo.ChooseCoachVO;
import com.andong.service.CoachService;
import com.andong.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.andong.domain.entity.ChooseCoach;
import com.andong.service.ChooseCoachService;
import com.andong.mapper.ChooseCoachMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 王海云
 * @description 针对表【choose_coach(教练选择表)】的数据库操作Service实现
 * @createDate 2024-03-20 17:59:29
 */
@Service
public class ChooseCoachServiceImpl extends ServiceImpl<ChooseCoachMapper, ChooseCoach>
        implements ChooseCoachService {
    @Resource
    private CoachService coachService;
    @Resource
    private StudentService studentService;

    @Override
    public Result insert(ChooseCoach chooseCoach) {
        // 查询有没有该学员的申请
        Long flag = lambdaQuery()
                .eq(ChooseCoach::getStudentId, chooseCoach.getStudentId())
                .eq(ChooseCoach::getStatus, 0)
                .count();
        if (flag > 0) {
            return Result.error("等待教练拒绝后才能再次申请！");
        }
        // 获取教练id
        Long coachId = chooseCoach.getCoachId();
        // 查询教练名下未毕业学员人数
        Long count = studentService.lambdaQuery()
                .eq(Student::getCoachId, coachId)
                .ne(Student::getStage, 5)
                .count();
        if (count >= 8) {
            return Result.error("该教练名下已有8名未毕业学员，请选择其他教练");
        }
        // 查询学员有没有被拒绝申请
        Long rejectFlag = lambdaQuery()
                .eq(ChooseCoach::getStudentId, chooseCoach.getStudentId())
                .eq(ChooseCoach::getStatus, 2)
                .count();
        if (rejectFlag > 0) {
            // 删除数据
            lambdaUpdate().eq(ChooseCoach::getStudentId, chooseCoach.getStudentId()).remove();
        }
        // 插入
        save(chooseCoach);
        return Result.success();
    }

    @Override
    public Result modify(ChooseCoach chooseCoach) {
        // 判断同意还是拒绝
        if (chooseCoach.getStatus() == 1) {
            // 同意, 设置学员的教练ID字段
            studentService.setCoach(chooseCoach.getStudentId(), chooseCoach.getCoachId());
            // 删除选择数据
            removeById(chooseCoach);
        } else {
            // 拒绝, 修改状态
            updateById(chooseCoach);
        }
        return Result.success();
    }

    @Override
    public Result selectByStudentId(Long studentId) {
        ChooseCoach chooseCoach = lambdaQuery().eq(ChooseCoach::getStudentId, studentId).one();
        if (chooseCoach == null) {
            return Result.success(null);
        }
        // 查询教练信息
        Coach coach = coachService.getById(chooseCoach.getCoachId());
        // 封装VO
        ChooseCoachVO chooseCoachVO = new ChooseCoachVO();
        // 将数据拷贝到VO中
        BeanUtil.copyProperties(chooseCoach, chooseCoachVO);
        chooseCoachVO.setCoachName(coach.getName());
        chooseCoachVO.setCoachPhone(coach.getPhone());
        return Result.success(chooseCoachVO);
    }

    @Override
    public Result selectByCoachId(Long coachId) {
        List<ChooseCoach> list = lambdaQuery().eq(ChooseCoach::getCoachId, coachId).eq(ChooseCoach::getStatus, 0).list();
        List<ChooseCoachVO> collect = list.stream().map(record -> {
            // 将数据拷贝到VO中
            ChooseCoachVO chooseCoachVO = new ChooseCoachVO();
            BeanUtil.copyProperties(record, chooseCoachVO);
            // 查询学员信息
            Student student = studentService.getById(record.getStudentId());
            chooseCoachVO.setStudentName(student.getName());
            chooseCoachVO.setStudentPhone(student.getPhone());
            chooseCoachVO.setStudentSex(student.getGender());
            chooseCoachVO.setStudentAge(student.getAge());
            chooseCoachVO.setStudentImage(student.getImage());
            return chooseCoachVO;
        }).toList();
        return Result.success(collect);
    }
}




